package springmvc;


import com.google.gson.Gson;
import org.json.JSONArray;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import springmvc.pojo.Person;
import springmvc.pojo.User;

import java.io.*;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

@Controller
public class MyController {
    @RequestMapping( "/")//负责主页面的访问
    public String index(){

        return "index";
    }
    @RequestMapping( "/temp")//网页测试区，忽略
    public String temp(){
        return "/page/table";
    }
    @RequestMapping("/target/{id}/{username}")
    public String toTarget(@PathVariable("id")Integer id ,@PathVariable("username")String username){
        System.out.println(id+"姓名"+username);
        return "target";
    }

    @RequestMapping(value="/findallEmp",produces="application/json;charset=UTF-8")
    @ResponseBody
    public  String findallEmp(Integer page, Integer limit){
        int before = limit * (page - 1) + 1;
        int after = page * limit;
        //带参数从数据库里查询出来放到eilist的集合里,这里使用生成数据
        Person person = new Person(  100,"張三" ,"男", "城市-0", "签名-0", 255, 24, 82830700, "作家", 15);
        List<Person> eilist = new ArrayList<Person>();
        eilist.add(person);

        JSONArray json = new JSONArray();
        json.put(eilist);
        int count = 1;
        Gson gson=new Gson();
        String js = gson.toJson(eilist);
        String jso = "{\"code\":0,\"msg\":\"\",\"count\":"+1000+",\"data\":"+js+"}";//转为layui需要的json格式，必须要这一步，博主也是没写这一步，在页面上数据就是数据接口异常
        System.out.println("这是要返回的数据"+jso);
        JSONController jsonController=new JSONController();
        jsonController.createJsonFile(jso,"D:\\JavaWebTest1\\springmvc2\\src\\main\\webapp\\WEB-INF\\templates\\layuimini-2A\\api\\table.json");
        return jso;
    }
}
